package com.situ.web.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.filters.AddDefaultCharsetFilter;
import org.omg.Messaging.SyncScopeHelper;

import com.situ.web.entity.Dept;
import com.situ.web.util.JDBCUtil;

public class DeptServlet extends HttpServlet{

	// 访问Servlet时候默认访问Service
	@Override
	protected void service(HttpServletRequest reqest, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("DeptServlet.service()");
		// 处理post请求乱码问题
		reqest.setCharacterEncoding("UTF-8");
		
		// /JavaWeb1906/dept?method=deleteById&id=5
		// /JavaWeb1906/dept?method=findAll
		String method = reqest.getParameter("method");
		switch (method) {
		case "deleteById":
			deleteById(reqest, response);
			break;
		case "findAll":
			findAll(reqest, response);
			break;
		case "add":
			add(reqest, response);
			break;
		default:
			break;
		}
		
	}
	
	private void add(HttpServletRequest reqest, HttpServletResponse response) throws ServletException, IOException {
		String name = reqest.getParameter("name");
		
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {	
			connection = JDBCUtil.getConnection();
			String sql = "INSERT INTO dept(name) VALUES(?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, name);
			System.out.println(preparedStatement);
			int count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("插入成功");
			} else {
				System.out.println("插入失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		
		// 重定向：/JavaWeb1906/dept?method=findAll
		response.sendRedirect("/JavaWeb1906/dept?method=findAll");
	}
	

	private void deleteById(HttpServletRequest reqest, HttpServletResponse response) throws ServletException, IOException {
		// "5"
		String id = reqest.getParameter("id");
		
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "DELETE FROM `dept` WHERE id=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, Integer.parseInt(id));
			System.out.println(preparedStatement);
			int count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("删除成功");
			} else {
				System.out.println("删除失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		
		// 重定向：/JavaWeb1906/dept?method=findAll
		response.sendRedirect("/JavaWeb1906/dept?method=findAll");
	}

	private void findAll(HttpServletRequest reqest, HttpServletResponse response) throws ServletException, IOException {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		ArrayList<Dept> list = new ArrayList<>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT id,name FROM dept";
			preparedStatement = connection.prepareStatement(sql);
			System.out.println(preparedStatement);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				// 每遍历一次封装一个Dept对象
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				Dept dept = new Dept(id, name);
				list.add(dept);
			}
			
			for (Dept dept : list) {
				System.out.println(dept);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		
		// 将list集合中数据转发到dept_list.jsp展示就可以
		reqest.setAttribute("list", list);
		reqest.getRequestDispatcher("/dept_list.jsp").forward(reqest, response);
	}
	
}
